//
//  ARefundViewController.m
//  JxCarios
//
//  Created by xcq on 15/3/4.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "ARefundViewController.h"
#import "CarAroundDataSource.h"
#import <ActionSheetStringPicker.h>

@interface ARefundViewController () <UITextFieldDelegate,UITextViewDelegate>
{
    CarAroundDataSource *carAournd;
    ActionSheetStringPicker *picker;
    NSMutableArray *yinhangArr;
}
@end

@implementation ARefundViewController


- (void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                name:UIKeyboardWillHideNotification object:nil];

    
}

- (void)viewDidDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    if (![_tuikuanyuanyin isFirstResponder]) {
        return;
    }

    NSDictionary *userInfo = [notification userInfo];
    
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGRect keyboardRect = [aValue CGRectValue];
    
    if (keyboardRect.size.height == 0) {
        return;
    }
    CGFloat height = CGRectGetHeight(self.view.frame) - (_tuikuanView.frame.origin.y + _tuikuanView.frame.size.height + 10);
    
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(0, - (CGRectGetHeight(keyboardRect) - height ), CGRectGetWidth(self.view.frame),  CGRectGetHeight(self.view.frame));
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    if (![_tuikuanyuanyin isFirstResponder]) {
        return;
    }
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(0, 0  , CGRectGetWidth(self.view.frame),  CGRectGetHeight(self.view.frame));
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self initAbout];
    [self setUp];
    [self netRequest];
}

- (void)netRequest {
    if (!carAournd) {
        carAournd = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    
    [carAournd getBankArr:^(id result) {
        NSArray *arr = [NSArray arrayWithArray:[result customObjectForkey:@"banklist"]];
        [self initDataSource:arr];
    } anfFail:^(id result) {
        
    }];
}

- (void)initDataSource:(NSArray *)arr {
     yinhangArr = [NSMutableArray new];
    for (NSDictionary *dic  in arr) {
        [yinhangArr addObject:[dic customObjectForkey:@"dic_value"]];
    }
}

- (void)initAbout {
    self.title = @"申请退款";
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
}

- (void)setUp {
    _shopName.text = [_info customObjectForkey:@"GOOD_NAME"];
    _shopPirce.text = [[_info customObjectForkey:@"ORDER_PRICE"] stringByAppendingString:@"元"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    if (textField.tag == 200) {
        NSCharacterSet *cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:Point]invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
        BOOL canChange = [string isEqualToString:filtered];
        if (canChange) {
            if (toBeString.length > 21) {
                return NO;
            }
        }
        return canChange;
    }
    
    return YES;
}



- (void)textViewDidBeginEditing:(UITextView *)textView {
    _textViewClues.hidden = YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length > 0) {
        
    }else {
        _textViewClues.hidden = NO;
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString * toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text]; //得到输入框的内容
    if (toBeString.length > 202) {
        return NO;
    }
    return YES;
    
}

/*
 * bank_open	..	开户行
 bank_card	..	银行卡号
 bank_name	..	开户人姓名
*/
- (IBAction)tuikuanAction:(id)sender {
    if (_yinhangCardNum.text.length  <= 0) {
        [self.view makeToast:@"请输入银行卡号" duration:1.5f position:TOP_POINT];
        return;
    }
    if(_yinhangCardNum.text.length < 15 || _yinhangCardNum.text.length > 19) {
        [self.view makeToast:@"银行卡号格式错误" duration:1.5f position:TOP_POINT];
        return;
    }
        
    if (_kaihuhang.text.length == 0) {
        [self.view makeToast:@"请选择银行" duration:1.5f position:TOP_POINT];
        return;
    }
    if (_zhihangName.text.length == 0) {
        [self.view makeToast:@"请输入开户支行" duration:1.5f position:TOP_POINT];
        return;
    }
    
    if (!carAournd) {
        carAournd = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    carAournd.zfb = @"";
    carAournd.refund_desc = _tuikuanyuanyin.text;
    carAournd.orderId = [_info customObjectForkey:@"ORDER_ID"];
    carAournd.bank_card = _yinhangCardNum.text;
    carAournd.bank_open = _kaihuhang.text;
    carAournd.bank_name = _zhihangName.text;
    [carAournd getTuiKuan:^(id result) {
        [self.view makeToast:@"退款成功" duration:1.5f position:TOP_POINT];
        [self performSelector:@selector(backToView) withObject:nil afterDelay:1.5f];
    } anfFail:^(id result) {
        
    }];
    
}
- (IBAction)seleteYh:(id)sender {
//kaihuhang
    if (yinhangArr.count == 0 || yinhangArr == nil) {
        [Common showAlert:@"提示" message:@"银行列表获取失败,请返回上级页面重新进入!"];
        return;
    }
      picker = [[ActionSheetStringPicker alloc]initWithTitle:@"银行选择" rows:yinhangArr initialSelection:0 doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
         _kaihuhang.text = yinhangArr[selectedIndex];
//        self.dataSource.drawtype = (int)selectedIndex ;
//        self.BankLabel.text = selectedValue;
    } cancelBlock:nil origin:self.view];
    [picker setCancelButton:[[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:nil action:nil]];
    [picker setDoneButton:[[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:nil action:nil]];
    [picker showActionSheetPicker];
    
}

- (void)actionSheetPickerDidSucceed:(AbstractActionSheetPicker *)actionSheetPicker origin:(id)origin {
    NSLog(@"选择了:%@",actionSheetPicker.title);
}
- (void)actionSheetPickerDidCancel:(AbstractActionSheetPicker *)actionSheetPicker origin:(id)origin {
    
}



- (void)backToView {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
